/*
 * Copyright 2009 Brook Smith brook.smith@paradise.net.nz.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.javagenerate.generator.script.gen;

import java.util.Properties;

import org.javagenerate.generator.script.BasePatternGenerator;
import org.javagenerate.generator.script.ClassDef;

public class GwtJdoGenSet extends BasePatternGenerator {

	public GwtJdoGenSet(Properties templateProperties, String patternName) {
		super(templateProperties, patternName);
	}

	@Override
	public ClassDef getClassdef(String type, String classname) {
		ClassDef classDef = null;
		if (type.equals("dto")) {
			return new DtoDef(classname, getPatternName());
		} else if (type.equals("jdo")) {
			return new JDODef(classname, getPatternName());
		} else if (type.equals("interface")) {
			return new DtoInterfaceDef(classname, getPatternName());
		} else if (type.equals("helper")) {
			return new DTOHelperDef(classname, getPatternName());
		} else if (type.equals("gwtasync")) {
			return new GwtAsyn(classname, getPatternName());
		} else if (type.equals("gwtservice")) {
			return new GwtService(classname, getPatternName());
		} else if (type.equals("gwtserver")) {
			return new GwtServer(classname, getPatternName());
		} else if (type.equals("jdodao")) {
			return new JdoDaoDef(classname, getPatternName());
		} else if (type.equals("nonbean")) {
			return new NonBeanClass(classname, getPatternName());
		} else if (type.equals("gwtedit")) {
			return new GwtEdit(classname, getPatternName());
		} else if (type.equals("gwtlist")) {
			return new GwtListDef(classname, getPatternName());
		}
		
		return getDefaultClassdef(type, classname);
	}

	@Override
	public void preWrite() {
		// validate
		if (getClass("interface") != null) {
			DtoInterfaceDef interfaceDef = (DtoInterfaceDef) getClass("interface");
			interfaceDef.getImports().addAll(getImports());

			System.out.println("Interface");

			if (getClass("jdo") != null) {
				JDODef jdoDef = (JDODef) getClass("jdo");
				jdoDef.getImports().addAll(getImports());
				System.out.println("jdo");

				if (getClass("helper") != null) {
					DTOHelperDef helperDef = (DTOHelperDef) getClass("helper");
					helperDef.setInterfaceName(interfaceDef.getClassName());
					helperDef.getImports().add(interfaceDef.getPackageName() + "." + interfaceDef.getClassName());
					System.out.println("helper");

					if (getClass("dto") != null) {
						ClassDef dtoDef = getClass("dto");
						dtoDef.getImports().addAll(getImports());
						dtoDef.getImports().add(interfaceDef.getPackageName() + "." + interfaceDef.getClassName());
						helperDef.getImports().add(dtoDef.getPackageName() + "." + dtoDef.getClassName());
						System.out.println("dto");

						if (getClass("jdodao") != null) {
							JdoDaoDef jdoDaoDef = (JdoDaoDef) getClass("jdodao");
							jdoDaoDef.getImports().add(jdoDef.getPackageName() + "." + jdoDef.getClassName());
							jdoDaoDef.getImports().add(dtoDef.getPackageName() + "." + dtoDef.getClassName());
							jdoDaoDef.setDtoHelperDef(helperDef);
							jdoDaoDef.setJdoDef(jdoDef);
							jdoDaoDef.setInterfaceDef(dtoDef);
							System.out.println("jdodao");

							if (getClass("gwtasync") != null) {
								GwtAsyn gwtAsyn = (GwtAsyn) getClass("gwtasync");
								gwtAsyn.setInterfaceName(dtoDef.getClassName());
								gwtAsyn.getImports().add(dtoDef.getPackageName() + "." + dtoDef.getClassName());
								
								gwtAsyn.getImports().add(getTemplateProperties().getProperty("base.package") + "." + "client.*");
								System.out.println("gwtasync");
								
								if (getClass("gwtservice") != null) {
									GwtService gwtClient = (GwtService) getClass("gwtservice");
									gwtClient.setInterfaceName(dtoDef.getClassName());
									gwtClient.getImports().add(dtoDef.getPackageName() + "." + dtoDef.getClassName());
									gwtClient.getImports().add(getTemplateProperties().getProperty("base.package") + "." + "client.*");

									System.out.println("gwtservice");

									if (getClass("gwtserver") != null) {
										GwtServer gwtServer = (GwtServer) getClass("gwtserver");

										gwtServer.setInterfaceName(dtoDef.getClassName());
										gwtServer.getImports().add(dtoDef.getPackageName() + "." + dtoDef.getClassName());
										gwtServer.getImports().add(gwtClient.getPackageName() + "." + gwtClient.getClassName());
										
										gwtServer.getImports().add(getTemplateProperties().getProperty("base.package") + ".server.*");
										gwtServer.getImplements().add(gwtClient.getClassName());
										
//										BaseService<"+interfaceName+">"
										
										gwtServer.setDaoDef(jdoDaoDef);
										System.out.println("gwtserver");
									}
									if (getClass("gwtedit") != null) {
										GwtEdit gwtEdit = (GwtEdit) getClass("gwtedit");
										gwtEdit.setGenClassName(dtoDef.getClassName());
										gwtEdit.setGenServiceAsync(gwtAsyn.getClassName());
										gwtEdit.setGenService(gwtClient.getClassName());
										
										gwtEdit.getImports().add(dtoDef.getPackageName() + "." + dtoDef.getClassName());
										gwtEdit.getImports().add(gwtAsyn.getPackageName() + "." + gwtAsyn.getClassName());
										gwtEdit.getImports().add(getTemplateProperties().getProperty("base.package") + "." + "client.*");
										System.out.println("gwtedit");
									}
									if (getClass("gwtlist") != null) {
										GwtListDef gwtList = (GwtListDef) getClass("gwtlist");
										gwtList.setInterfaceDef(dtoDef);
										gwtList.setServiceAsync(gwtAsyn);
										gwtList.getImports().add(getTemplateProperties().getProperty("base.package") + "." + "client.*");
										System.out.println("gwtList");
									}

								}
							}
						}
					}
				}

			}
		}
		if (getClass("nonbean") != null) {

		}
	}
}
